﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Utility;
namespace csla4Skeleton.UI.SL.TongHop.TH_KyKetChuyen
{
    public partial class TH_KyKetChuyen_MainWindow : UserControl, Csla.Xaml.ISupportNavigation
    {
        #region ISupportNavigation Members

        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    VoucherId = null;
                else
                    VoucherId = Int32.Parse(parameters);
            }
            catch
            {
                VoucherId = null;
            }
        }

        public string Title
        {
            get { return "Ky Ket Chuyen"; }
        }

        #endregion

        #region Load Methods
        private System.Nullable<int> VoucherId = null;

        public TH_KyKetChuyen_MainWindow(int VoucherId)
            : this()
        {
            this.VoucherId = VoucherId;
        }

        public TH_KyKetChuyen_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.TongHop.TH_KyKetChuyen.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);

            InitializeComponent();

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource orderCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["orderViewModelSource"];
                if (VoucherId == null)
                {
                    TH_KyKetChuyen_ViewModel orderViewModel = new TH_KyKetChuyen_ViewModel();
                    orderViewModel.ChildList_AddedNew += new TH_KyKetChuyen_ViewModel.ChildList_AddedNew_EventHandler(orderViewModel_ChildList_AddedNew);
                    orderCollectionViewSource.Source = new List<object> { orderViewModel };
                }
                else
                {
                    TH_KyKetChuyen_ViewModel orderViewModel = new TH_KyKetChuyen_ViewModel((int)VoucherId);
                    orderViewModel.ChildList_AddedNew += new TH_KyKetChuyen_ViewModel.ChildList_AddedNew_EventHandler(orderViewModel_ChildList_AddedNew);
                    orderCollectionViewSource.Source = new List<object> { orderViewModel };
                }

                this.OrderDetailsGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.OrderDetailsGridView);
                this.TH_KyKetChuyen_Ma_TextBox.Focus();
            }
        }

        #endregion

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                TH_KyKetChuyen_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.OrderDetailsTab.IsSelected = true;
                }
            }
            e.Handled = true;
        }
        void AddItems()
        {
            if (this.OrderDetailsTab.IsSelected)
            {
                AddOrderDetailsItem();
            }
        }
        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, null);
        }
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
            Dispatcher.BeginInvoke(new Action(() =>
            {
                grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[1]);
                grid.Focus();
                grid.BeginEdit();
            }));
        }

        private RadGridView GetActiveGridView()
        {
            if (this.OrderDetailsTab.IsSelected)
            {
                return OrderDetailsGridView;
            }
            return null;
        }

        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
            TH_KyKetChuyen_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            AddItems();
        }
        #endregion

        #region View Model Related Methods
        private TH_KyKetChuyen_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource orderCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["orderViewModelSource"];
            return ((List<object>)orderCollectionViewSource.Source)[0] as TH_KyKetChuyen_ViewModel;
        }
        private void AddOrderDetailsItem()
        {
            GetViewModel().AddOrderDetailsItem();
        }

        void orderViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "TH_KyKetChuyen_ChiTiets")
            {
                this.OrderDetailsGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
        }

        #endregion

        private void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
        }

        private void TransferredItemButton_Click(object sender, RoutedEventArgs e)
        {
            //this.TransferredItemButton.IsEnabled = false;
            //this.DeleteTransferredtemButton.IsEnabled = true;
            GetViewModel().TransferInBookVoucher();
        }

        private void DeleteTransferredtemButton_Click(object sender, RoutedEventArgs e)
        {
            //this.TransferredItemButton.IsEnabled = true;
            //this.DeleteTransferredtemButton.IsEnabled = false;
            GetViewModel().RemoveInBookVoucher();
        }

        private void CreateNewtemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().NewTH_KyKetChuyen_ViewModel();
        }
    }
}
